package adb.group.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.OutPatient;
import beans.Treatment;

/**
 * Servlet implementation class test
 */
public class test extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public test() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection connection = (Connection) DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:ORCL", "dbfp_dba",
					"dbfpdba");
			Statement statement = (Statement) connection.createStatement();

			// OutPatient Table
			ResultSet outPatientResult = statement
					.executeQuery("select * from outpatient_t");
			ArrayList<OutPatient> outPatientList = new ArrayList<OutPatient>();
			if (outPatientResult == null) {
				response.sendRedirect("Index.jsp ");
			} else {
				while (outPatientResult.next()) {
					OutPatient outPatient = new OutPatient();
					outPatient.setPatientID(outPatientResult.getString(1));
					outPatient.setVisit_date(outPatientResult.getDate(2));
					outPatientList.add(outPatient);
				}
				request.getSession().setAttribute("outPatientList", outPatientList);
			}

			// Treatment Table
			ResultSet treatmentResult=null;
			ArrayList<Treatment> treatmentList = new ArrayList<Treatment>();
			if (request.getParameter("type").equals("1")) {
				treatmentResult = statement
						.executeQuery("select * from treatment_t where physician_ID='"
								+ request.getSession().getAttribute("workID")
								+ "'");
				while (treatmentResult.next()) {
					Treatment treatment = new Treatment();
					treatment.setPatientID(treatmentResult.getString(3));
					treatment.setPhysicianID(treatmentResult.getString(4));
					treatment.setTreatmentDate(treatmentResult.getDate(2));
					treatment.setTreatmentID(treatmentResult.getString(1));
					treatment.setPhysicianName((String) request.getSession()
							.getAttribute("name"));
					treatmentList.add(treatment);
				}
				response.sendRedirect("/project/resources/physician/viewTreatmentHistory.jsp");
			}else if(request.getParameter("type").equals("2")){
				treatmentResult = statement
						.executeQuery("select * from treatment_t where patient_ID='"
								+ request.getSession().getAttribute("workID")
								+ "'");
				
				while (treatmentResult.next()) {
					Treatment treatment = new Treatment();
					treatment.setPatientID(treatmentResult.getString(3));
					treatment.setPhysicianID(treatmentResult.getString(4));
					treatment.setTreatmentDate(treatmentResult.getDate(2));
					treatment.setTreatmentID(treatmentResult.getString(1));
					treatment.setPhysicianName((String) request.getSession()
							.getAttribute("name"));
					treatmentList.add(treatment);
				}
				response.sendRedirect("/project/resources/patient/viewTreatmentHistory.jsp");
			}
			request.getSession().setAttribute("treatmentList", treatmentList);
			// Forward
			
		} catch (Exception e) {
			System.out.print(e);
		}

		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
